package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.payment.pojo.dto.PaymentDTO;
import com.xbongbong.pro.payment.pojo.dto.RelationshipGetDTO;
import com.xbongbong.pro.payment.pojo.vo.RelationshipGetVO;
import com.xbongbong.pro.sheet.pojo.dto.SheetAmountDetailGetDTO;
import com.xbongbong.pro.sheet.pojo.vo.SheetAmountDetailGetVO;
import com.xbongbong.saas.help.SaasPaymentHelper;
import com.xbongbong.saas.service.PaymentService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 回款
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/14 17:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private SaasPaymentHelper saasPaymentHelper;

    /**
     * 回款批量操作
     * @param paymentDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/operateBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String payment(@RequestBody @Valid PaymentDTO paymentDTO, BindingResult br) {
        XbbResponse<FormDataAddVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(paymentDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(paymentDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                FormDataAddVO paymentVO = paymentService.pay(paymentDTO);
                response = new XbbResponse<>(paymentVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 回款单金额明细
     * @param sheetAmountDetailGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/11/12 012 16:10
     * @update 19/11/12 012 16:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/amount/detail/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paymentAmountDetailGet(@RequestBody @Valid SheetAmountDetailGetDTO sheetAmountDetailGetDTO, BindingResult br) {
        XbbResponse<SheetAmountDetailGetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SheetAmountDetailGetVO sheetAmountDetailGetVO = saasPaymentHelper.paymentAmountDetailGet(false, sheetAmountDetailGetDTO);
                response = new XbbResponse<>(sheetAmountDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 通过合同ids获取其关联的应收款,回款单或 合同信息
     * @param relationshipGetDTO
     * @param br
     * @return java.lang.String
     * @author 余乾
     * @date 19/11/12 012 16:10
     * @update 19/11/12 012 16:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/relationshipInfo/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractOrPaymentGet(@RequestBody @Valid RelationshipGetDTO relationshipGetDTO, BindingResult br) {
        XbbResponse<RelationshipGetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RelationshipGetVO relationshipGetVO = paymentService.getContractOrPaymentOrPaymentSheetInfo(relationshipGetDTO);
                response = new XbbResponse<>(relationshipGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
